package com.veetao.api.resp;

import java.io.Serializable;

import com.veetao.api.annotation.Desc;
import com.veetao.api.annotation.Required;
import com.veetao.api.errcode.ErrorCodeMessage;

/**
 * @author Marshal(imdeep@gmail.com) Initial Created at 2014年8月25日
 */
@Desc("通用操作结果")
public class CommonResultResp implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 默认的成功返回 */
    public static final transient CommonResultResp SUCCESS = CommonResultResp.buildBaseSuccResult();

    /** 默认的失败返回 */
    public static final transient CommonResultResp FAILED = CommonResultResp.buildFailResult(ErrorCodeMessage.MESSAGE_CODE_DEFAULT);

    @Required(value = true, desc = "操作结果，0为成功,data字段包含数据（对象或数组）； 其它值为失败,msg 返回错误原因")
    private int state;



    public CommonResultResp(boolean succ) {
    	this.state = succ? 0 : 1;
	}



	public static final CommonResultResp buildFailResult(String code) {
        return new FailResultResp(code,ErrorCodeMessage.getMessage(code));
    }
	
	public static final CommonResultResp buildFailResult(String code, String customMessage) {
        return new FailResultResp(code,customMessage);
    }
	
	public static final CommonResultResp buildDefaultCodeFailResult(String customMessage) {
        return buildFailResult("100000",customMessage);
    }

	public static final CommonResultResp buildBaseSuccResult() {
        return new CommonResultResp(true);
    }


	public int getState() {
		return state;
	}



	public void setState(int state) {
		this.state = state;
	}



	public CommonResultResp() {
		this(true);
	}
	
	
	public static final CommonResultResp buildSuccessResult(String code, String customMessage) {
        return new SuccessResultResp(code,customMessage);
    }
       
}
